<template>
  <a-card :bordered="false">
    <super-query :fieldList="tableObj.columns" @ok="handleSuperQuery"/>
    <v-table
      ref="table"
      :data="loadData"
      :columns="tableObj.columns"
    >
      <template v-slot:buttons>
        <a-button @click="$refs.plandayForm.init('','add')" icon="plus" type="primary">新增日计划</a-button>
      </template>
      <span slot='productCount' slot-scope="{ row,text }">
        <a @click='check(row)'>{{text}}</a>
      </span>
      <span slot="action" slot-scope="{ row,text }">
        <span v-if='row.statusCode === "0"'>
          <a href="javascript:;" @click="edit(row)">编辑</a>
          <a-divider type="vertical" />
        </span>
        <span v-if='row.statusCode === "0"'>
          <a-popconfirm title="发布工单" @confirm="release(row)" okText="是" cancelText="否">
            <a href="javascript:;">发布工单</a>
          </a-popconfirm>
          <a-divider type="vertical" />
        </span>
        <span v-if='row.statusCode === "10"'>
          <a-popconfirm title="是否强制关闭工单" @confirm="stop(row)" okText="是" cancelText="否">
            <a href="javascript:;">关闭工单</a>
          </a-popconfirm>
          <a-divider type="vertical" />
        </span>
        <span>
          <a href="javascript:;" @click="showDetail(row)">查看详情</a>
        </span>
        <span>
           <a-divider type="vertical" />
          <a href="javascript:;" @click="generateMat(row)">物料需求清单</a>
        </span>
<!--        <span>-->
<!--           <a-divider type="vertical" />-->
<!--          <a href="javascript:;" @click="check(row)">齐套检查</a>-->
<!--        </span>-->
      </span>
    </v-table>
    <plan-day-form
      @ok="queryPage"
      ref='plandayForm'
    />
    <plan-detail ref='planDetail'></plan-detail>
    <check ref='check'></check>
  </a-card>
</template>

<script>
import { fetchList, release, stop, generateMat,  } from '@/api/plan/playday'
import { TableMixin } from '@/mixins/TableMixin'
import { statusTypeList } from '@/dict'
import SuperQuery from '@comp/SuperQuery/SuperQuery'
import { tableObj } from './template'
import PlanDayForm from './plandayForm'
import planDetail from './detail/index'
import check from './check'
export default {
  name: 'PlanDay',
  mixins: [TableMixin],
  components: {
    PlanDayForm,
    SuperQuery,
    planDetail,
    check
  },
  data () {
    return {
      tableObj,
      statusTypeList,
      dataSource: [],
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    edit (record) {
      this.$refs.plandayForm.init(record.id, 'edit')
    },
    release(row){
      release({
        planCode: row.planCode
      }).then(res => {
        this.$message.success("工单发布成功")
        this.queryPage()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    stop(row){
      stop(row.planCode).then(res => {
        this.$message.success("工单关闭成功")
        this.queryPage()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 查看详情
    showDetail(row){
      this.$refs.planDetail.init(row)

    },
    generateMat(row){
      generateMat(row.planCode).then(res => {
        this.$message.success("生成成功")
        this.queryPage()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    check(row){
      this.$refs.check.init(row.planCode)
    },
    handleSuperQuery (params) {
      console.log(params,'params')
      if(params){
        this.queryParam.queryType = params.queryType
        this.queryParam.queryItems = encodeURI(JSON.stringify(params.queryItems))
      }else{
        this.queryParam.queryType = ''
        this.queryParam.queryItems = ''
      }
      this.queryPage()
    }
  }
}
</script>

<style scoped>

</style>
